﻿using System.Collections.ObjectModel;
using System.Windows.Automation;
using ProdControls.Interfaces;
using ProdControls.UIAPatterns;
using ProdLogging;

namespace ProdControls.Bridges
{
    internal static class TableItemBridge
    {
        #region Required

        /// <summary>
        ///   Gets a collection of UI Automation providers that represents all the column headers in a DataGrid
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <returns> An array of header items </returns>
        public static AutomationElement[] GetColumnHeadersHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetColumnHeaders(control);
        }

        private static AutomationElement[] UiaGetColumnHeaders(BaseProdControl control)
        {
            AutomationElement[] retVal = TablePatternHelper.GetColumnHeaders(control.UiaElement);
            Collection<object> retList = new Collection<object>();
            foreach (AutomationElement item in retVal)
            {
                retList.Add(item);
            }

            LogController.ReceiveLogMessage(new LogMessage("Headers", retList));

            return retVal;
        }

        /// <summary>
        ///   Gets a collection of UI Automation providers that represents all the row headers in a DataGrid
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <returns> An array of header items </returns>
        public static AutomationElement[] GetRowHeadersHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetRowHeaders(control);
        }

        private static AutomationElement[] UiaGetRowHeaders(BaseProdControl control)
        {
            AutomationElement[] retVal = TableItemPatternHelper.GetRowHeaderItems(control.UiaElement);
            Collection<object> retList = new Collection<object>(retVal);

            LogController.ReceiveLogMessage(new LogMessage("Headers", retList));

            return retVal;
        }

        /// <summary>
        ///   Gets a UI Automation element that supports the GridPattern control pattern and represents the table cell or item container.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The TableItem Element. </param>
        /// <returns> A UI Automation element that supports the GridPattern control pattern and represents the table cell or item container </returns>
        public static AutomationElement GetContainingGridHook(this ITableItem extension, BaseProdControl control)
        {
            return TableItemPatternHelper.GetContainingGrid(control.UiaElement);
        }

        #endregion Required

        /// <summary>
        ///   Gets the ordinal number of the column that contains the cell or item
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <returns> A zero-based ordinal number that identifies the column containing the cell or item </returns>
        public static int GetColumnIndexHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetColumnIndex(control);
        }

        private static int UiaGetColumnIndex(BaseProdControl control)
        {
            int retVal = TableItemPatternHelper.GetColumnIndex(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Column Index: " + retVal));

            return retVal;
        }

        /// <summary>
        ///   Gets the number of columns spanned by a cell or item.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The TableItem element. </param>
        /// <returns> The number of columns spanned </returns>
        public static int GetColumnSpanHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetColumnSpan(control);
        }

        private static int UiaGetColumnSpan(BaseProdControl control)
        {
            int retVal = TableItemPatternHelper.GetColumnSpan(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Column Span: " + retVal));

            return retVal;
        }

        /// <summary>
        ///   Gets the ordinal number of the row that contains the cell or item.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The TableItem element. </param>
        /// <returns> A zero-based ordinal number that identifies the row containing the cell or item </returns>
        public static int GetRowIndexHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetRowIndex(control);
        }

        private static int UiaGetRowIndex(BaseProdControl control)
        {
            int retVal = TableItemPatternHelper.GetRowIndex(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Row Index: " + retVal));

            return retVal;
        }

        /// <summary>
        ///   Gets the number of rows spanned by a cell or item.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The TableItem Element. </param>
        /// <returns> The number of rows spanned </returns>
        public static int GetRowSpanHook(this ITableItem extension, BaseProdControl control)
        {
            return UiaGetRowSpan(control);
        }

        private static int UiaGetRowSpan(BaseProdControl control)
        {
            int retVal = TableItemPatternHelper.GetRowSpan(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Row Span: " + retVal));

            return retVal;
        }
    }
}